//
//  FYComposeToolBar.swift
//  Mircore
//
//  Created by apple on 16/8/16.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit


//toolbar按钮的枚举类型

enum FYComposeToolBarButtonType: Int {
    
    //图片
    case Picture  = 0
    
    //@
    case Mention  = 1
    
    //#
    case  Trend = 2
    
    //表情
    
    case  Emoticon = 3
    
    //加号
    
    case Add = 4
}



//自定义发微博toolbar

//UIStackView 是容器  不具备渲染功能 颜色根据子控件显示


class FYComposeToolBar: UIStackView {

  //点击按钮需要执行的闭包
    
    var  didSelecteToolBarButtonClosure:((type: FYComposeToolBarButtonType) -> ())?
    
    
    //表情按钮
    
    var emoticonButton: UIButton?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
   
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder: ) has  not  been  implemented")
    }
    
    
    //添加控件设置布局方式
    
    private func  setupUI() {
    
    //水平方向布局
        
        axis = .Horizontal
        
        //子控件等比填充
        
        distribution  = .FillEqually
        
        
        //创建按钮
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        
        addChildButton("compose_mentionbutton_background", type: .Mention)
        
        addChildButton("compose_trendbutton_background", type: .Trend)
        
        emoticonButton =  addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        
        addChildButton("compose_add_background", type: .Add)
    
    
    
    }
    
    
    //创建子控件的通用方法
    
    private  func  addChildButton(imageName: String, type: FYComposeToolBarButtonType) -> UIButton {
    
    let  button = UIButton()
        
        //根据枚举原始值  作为按钮的tag
        
        button.tag  = type.rawValue
        
        //添加点击事件
        
        button.addTarget(self, action: #selector(FYComposeToolBar.buttonAction(_:)), forControlEvents: .TouchUpInside)
        
        //设置图片
        
        button.setImage(UIImage(named: imageName), forState: .Normal)
        
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
       
        //设置背景图片
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        
        
        
        //取消高亮
        
        button.adjustsImageWhenHighlighted  = false
        
        //添加按钮
        
        addArrangedSubview(button)
        
        return button
    }
    
    //MARK:-  点击事件
    
    @objc  private  func  buttonAction(button: UIButton)  {
    
      //通过原始值获取枚举
        
        let  type  = FYComposeToolBarButtonType(rawValue: button.tag)!
        
        //执行闭包  传入参数(枚举)
        
        didSelecteToolBarButtonClosure?(type: type)
     
    }
    
    //根据外界传入的键盘类型设置不同的icon
    
    func  switchEmotionIcon(isEmoticon: Bool) {
    
    //如果外界告诉我们是表情键盘类型  那么显示系统键盘的icon  否则显示表情的icon
        
        
        if isEmoticon {
            
            //显示系统键盘的icon
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
            
        }else  {
        
             //显示表情键盘的icon
            
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
        
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
    
    
    }
    
}
